package com.ruoyi.quartz.task;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.compensationManagement.domain.CompensationRetireeSubsidyProject;
import com.ruoyi.compensationManagement.domain.CompensationRetirespayroll;
import com.ruoyi.compensationManagement.service.ICompensationRetireeSubsidyProjectService;
import com.ruoyi.compensationManagement.service.ICompensationRetirespayrollService;
import com.ruoyi.system.service.ISysCommonService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 退休人员补贴工资
 * （推荐放在工资表后执行）
 *
 * @author zy
 */
@Component("retirespayrollTasks")
public class retirespayrollTasksRegular {
    @Autowired
    private ICompensationRetireeSubsidyProjectService compensationRetireeSubsidyProjectService;

    @Autowired
    private ICompensationRetirespayrollService compensationRetirespayrollService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysCommonService sysCommonService;

    //生成每月退休人员补贴工资
    public void generateRetireeWageSubsidies(String nowDateStr) throws ParseException {
        Date nowDate = DateUtils.parseDate(nowDateStr, "yyyy-MM");
        List<String> errMesg = new ArrayList<>();
        List<SysUser> userList = sysUserService.selectAllRetireUserList();
        List<CompensationRetireeSubsidyProject> list = compensationRetireeSubsidyProjectService.selectCompensationRetireeSubsidyProjectList(null);
        List<CompensationRetireeSubsidyProject> allFloatingItems = compensationRetireeSubsidyProjectService.getAllFloatingItems();
        for (SysUser user : userList) {
            //非管理员
            if (user.getUserId() != 1&& user.getStatus().equals("0")&&user.getDelFlag().equals("0")) {
                //应发工资
                BigDecimal wagesPayable = new BigDecimal("0");
                CompensationRetirespayroll compensationRetirespayroll = new CompensationRetirespayroll();
                compensationRetirespayroll.setJobNumber(user.getJobNumber());
                compensationRetirespayroll.setNickName(user.getNickName());
                compensationRetirespayroll.setRetireespayrollDate(nowDate);
                compensationRetirespayroll.setFloatingItems(allFloatingItems);
                //浮动项目总工资
                for (CompensationRetireeSubsidyProject floatItem : allFloatingItems) {
                    wagesPayable = wagesPayable.add(floatItem.getSubsidyprojectMoney());
                }

                //固定项目
                for (CompensationRetireeSubsidyProject fixedItems : list) {
                    if (fixedItems.getSubsidyprojectName().equals("生活补贴")) {
                        compensationRetirespayroll.setLivingallowance(fixedItems.getSubsidyprojectMoney());
                        wagesPayable = wagesPayable.add(fixedItems.getSubsidyprojectMoney());
                    } else if (fixedItems.getSubsidyprojectName().equals("住房改革补贴")) {
                        compensationRetirespayroll.setHousingreformsubsidies(fixedItems.getSubsidyprojectMoney());
                        wagesPayable = wagesPayable.add(fixedItems.getSubsidyprojectMoney());
                    } else if (fixedItems.getSubsidyprojectName().equals("退休人员慰问金")) {
                        compensationRetirespayroll.setSolatiumforretirees(fixedItems.getSubsidyprojectMoney());
                        wagesPayable = wagesPayable.add(fixedItems.getSubsidyprojectMoney());
                    }
                }
                compensationRetirespayroll.setRetireesPayrollTotal(wagesPayable);
                //插入数据库
                compensationRetirespayrollService.insertCompensationRetirespayroll(compensationRetirespayroll);
            }

        }
    }
}
